package com.kyzh.core

import android.app.Activity
import android.content.Context
import android.util.Log
import cn.jiguang.verifysdk.api.JVerificationInterface
import com.github.gzuliyujiang.oaid.DeviceIdentifier
import com.gushenge.atools.ATools
import com.gushenge.core.*
import com.gushenge.core.dao.MMKVConsts
import com.hjq.language.MultiLanguages
import com.hjq.toast.ToastUtils
import com.kingja.loadsir.core.LoadSir
import com.kyzh.core.utils.ToastStyle
import com.kyzh.core.utils.appinit.appInit
import com.kyzh.core.utils.loadsir.LoadingCallback
import com.kyzh.core.utils.im.TUIUtils
import com.kyzh.core.utils.loadsir.EmptyCallback
import com.kyzh.core.utils.loadsir.ErrorCallback
import com.tencent.imsdk.v2.V2TIMSDKListener
import com.tencent.imsdk.v2.V2TIMUserFullInfo
import com.umeng.commonsdk.UMConfigure
import com.umeng.socialize.PlatformConfig
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.UMShareConfig
import okhttp3.OkHttpClient
import rxhttp.RxHttpPlugins
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.concurrent.TimeUnit
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.X509TrustManager


/**
 * Created by Administrator on 2017/8/9 0009.
 */

class MyApplication : CoreApplication() {

    override fun onCreate() {
        super.onCreate()
        instance = this
        val with =
            with(getSharedPreferences("spSurePactfile", Context.MODE_PRIVATE)) {
                getBoolean("spSurePact", false)
            }
        UMConfigure.preInit(this, "5c7e26653fc19501f3000c3c", "umeng")
        LoadSir.beginBuilder()
            .addCallback(EmptyCallback())
            .addCallback(LoadingCallback())
            .addCallback(ErrorCallback())
            .setDefaultCallback(LoadingCallback::class.java) //设置默认状态页
            .commit()

        // 多语言注册
        MultiLanguages.init(this)
        // 工具
        ATools.init(this)
        if(with) {
            appInit{}
        }

        // 初始化吐司
        ToastUtils.init(this, ToastStyle())
    }


    class TrustAllCerts : X509TrustManager {
        override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
        override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
        override fun getAcceptedIssuers(): Array<X509Certificate> {
            return arrayOf()
        }
    }

    public fun createSSLSocketFactory(): SSLSocketFactory {
        var ssfFactory: SSLSocketFactory? = null
        try {
            val sc: SSLContext = SSLContext.getInstance("TLS")
            sc.init(null, arrayOf(TrustAllCerts()), SecureRandom())
            ssfFactory = sc.getSocketFactory()
        } catch (e: Exception) {
        }
        return ssfFactory!!
    }
    companion object {
        val weixin = "wx584bf861807fdf4f"
        // 客户如更换qqzone 时  需全局替换
        val qqzone = "102095944"
        var conIM = false
        var quanSelect = 0
        fun init(activty: Activity, appid:Int) {
            TUIUtils.init(instance, appid, null, object : V2TIMSDKListener(){
                override fun onConnecting() {
                    //                onConnecting	正在连接到腾讯云服务器	适合在 UI 上展示“正在连接”状态。
                    Log.e(TUIUtils.TAG, "onConnecting: ", )
                }

                override fun onConnectSuccess() {
                    //                onConnectSuccess	已经成功连接到腾讯云服务器	-
                    conIM = true
                    Log.e(TUIUtils.TAG, "onConnectSuccess: ", )
                }

                override fun onConnectFailed(code: Int, error: String?) {
                    //                onConnectFailed	连接腾讯云服务器失败	可以提示用户当前网络连接不可用。
                    Log.e(TUIUtils.TAG, "onConnectFailed: code:${code}  error:${error}", )
//                    MMKVConsts.uid = ""
                    "当前网络连接不可用".toast()

                }

                override fun onKickedOffline() {
                    //                onKickedOffline	当前用户被踢下线	此时可以 UI 提示用户“您已经在其他端登录了当前帐号，是否重新登录？”
                    Log.e(TUIUtils.TAG, "onKickedOffline: ", )
//                    "您已经在其他端登录了当前帐号,请重新登录".toast()
//                    MMKVConsts.uid = ""
//                    activty.startActivity<LoginActivity>()
                }

                override fun onUserSigExpired() {
                    //                onUserSigExpired	登录票据已经过期	请使用新签发的 UserSig 进行登录。
                    Log.e(TUIUtils.TAG, "onUserSigExpired: ", )
//                    "登录信息已失效,请重新登录".toast()
//                    MMKVConsts.uid = ""
//                    activty.startActivity<LoginActivity>()
                }

                override fun onSelfInfoUpdated(info: V2TIMUserFullInfo?) {
                    //                onSelfInfoUpdated	当前用户的资料发生了更新	可以在 UI 上更新自己的头像和昵称。
                    Log.e(TUIUtils.TAG, "onSelfInfoUpdated: ", )
                }
            })
        }
        lateinit var instance: MyApplication
            private set
    }

    override fun attachBaseContext(base: Context?) {
        // 绑定语种
        super.attachBaseContext(MultiLanguages.attach(base))
    }
}
